
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jl_plugin/bean/BlueDevice.dart';
import 'package:jl_plugin/jl_plugin.dart';
import 'package:jl_plugin_example/AppBarLeftTitle.dart';
import 'package:jl_plugin_example/BlueDetailPage.dart';

class BlueScanListPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<BlueScanListPage>{
  List<BlueDevice> blueDevices=[];
  @override
  void initState() {
    super.initState();
    JlPlugin.instance.addScanListener(blueScanListener);
    JlPlugin.instance.startScanBlue();
  }

  void blueScanListener(BlueDevice devices) async{
    setState(() {
      blueDevices.add(devices);
    });
  }

  @override
  void dispose() {
    super.dispose();
    JlPlugin.instance.stopScanBlue();
    JlPlugin.instance.removeScanListener(blueScanListener);
  }

  void againScan() async{
    setState(() {
      blueDevices.clear();
    });
    await JlPlugin.instance.stopScanBlue();
    await JlPlugin.instance.startScanBlue();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLeftTitle(title: "蓝牙搜索",rightWidget: Row(children: [
        InkWell(onTap: (){
          againScan();
        },child: Text("重新搜索"))
      ],),),
      body: ListView.builder(itemBuilder: (context,index){
        var device=blueDevices[index];
        return InkWell(onTap: () async{
          await JlPlugin.instance.stopScanBlue();
          Navigator.push(context, MaterialPageRoute(builder: (context)=>BlueDetailPage(device)));
        },child: BlueDeviceItem(device: device,));
      },itemCount: blueDevices.length,),
    );
  }

}

class BlueDeviceItem extends StatelessWidget {
  final BlueDevice device;

  const BlueDeviceItem({super.key, required this.device});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      decoration: const BoxDecoration(
        border: Border(bottom: BorderSide(color: Colors.grey, width: 0.3)),
      ),
      child: Row(
        children: [
          const Icon(Icons.bluetooth, color: Colors.blue),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  device.name.isNotEmpty ? device.name : "未知设备",
                  style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 4),
                Text(
                  device.address,
                  style: TextStyle(fontSize: 12, color: Colors.grey[600]),
                )
              ],
            ),
          ),
          Text(
            "RSSI: ${device.rssi}dBm",
            style: TextStyle(
              fontSize: 12,
              color: device.rssi > -70 ? Colors.green : Colors.red,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }
}